﻿using System;
using System.Data;
using System.Windows.Forms;
using HRM.HRMServiceProxy;
using WeifenLuo.WinFormsUI.Docking;
using System.Drawing;
using HRM.BusinessLogic;

namespace HRM
{
    public partial class frmGetNewDataList_TNN : DockContent
    {
        DialogResult dlgResult;
        RoleRightInfo listRole;
        int itemCount = 0;
        int pageSize = UIParamater.pageSize;
        int totalPage = 0;
        int currPage = 0;
        int branch_ID;
        int department_ID;
        string _selectedID = ",";
        DataTable _dtDanhSachCanhBaoNghe = null;

        public DataTable dtDanhSachCanhBaoNghe
        {
            get { return _dtDanhSachCanhBaoNghe; }
            set { _dtDanhSachCanhBaoNghe = value; }
        }

        public string SelectedID
        {
            get { return _selectedID; }
            set { _selectedID = value; }
        }
        private string _FullName;
        public string FullName
        {
            get { return _FullName; }
            set { _FullName = value; }
        }
        string _Code;
        public string Code
        {
            get { return _Code; }
            set { _Code = value; }
        }
        private int _EmployeeID;
        public int EmployeeID
        {
            get { return _EmployeeID; }
            set { _EmployeeID = value; }
        }
        string _SIBookNo;
        public string SIBookNo
        {
            get { return _SIBookNo; }
            set { _SIBookNo = value; }
        }
        

        EmployeeClient _wcfEmployee;
        public frmGetNewDataList_TNN()
        {
            InitializeComponent();
            dlgResult = DialogResult.Cancel;

            _wcfEmployee = Service.Employee();
            txtKeyWord.Focus();
            

            branch_ID = Program.intBranchID;
            BuildVoucherGrid();
            LoadData();
            cboSoThang.SelectedIndex = 1;
        }
        public frmGetNewDataList_TNN(RoleRightInfo _listRole)
        {
            InitializeComponent();
            dlgResult = DialogResult.Cancel;

            _wcfEmployee = Service.Employee();
            txtKeyWord.Focus();
            listRole = _listRole;

            branch_ID = Program.intBranchID;
            BuildVoucherGrid();
            LoadData();
            cboSoThang.SelectedIndex = 1;
        }
        private void BuildVoucherGrid()
        {
            dgvEmployeeList.Columns.Clear();
            //Thêm cột Checkbox
            DataGridViewCheckBoxColumn col = new DataGridViewCheckBoxColumn();
            col.DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleCenter;
            col.Name = "Check";
            col.HeaderText = "Chọn";
            col.Width = 50;
            col.HeaderCell.Style.Alignment = DataGridViewContentAlignment.MiddleLeft;
            dgvEmployeeList.Columns.Add(col);
            col.Visible = true;

            UISetting.AddColumn(ref dgvEmployeeList, "RowNumber", "STT", "C", 50, true, true);
            UISetting.AddColumn(ref dgvEmployeeList, "Employee_ID", "Mã chính", "", 1, false, true);
            UISetting.AddColumn(ref dgvEmployeeList, "Employee_FullName", UpdateUI.GetString("hovaten"), "", 150, true, true);
            UISetting.AddColumn(ref dgvEmployeeList, "Employee_DateOfBirth", UpdateUI.GetString("ngaysinh"), "", 90, true, true);
            UISetting.AddColumn(ref dgvEmployeeList, "Detail_InsBookNo", UpdateUI.GetString("sosobh"), "C", 90, true, true);
            UISetting.AddColumn(ref dgvEmployeeList, "Detail_FromDate", UpdateUI.GetString("tuthang"), "C", 90, true, true);
            UISetting.AddColumn(ref dgvEmployeeList, "Detail_ToDate", UpdateUI.GetString("denthang"), "C", 90, true, true);
            UISetting.AddColumn(ref dgvEmployeeList, "Detail_TotalMonth", "Số tháng", "C", 90, true, true);
            UISetting.AddColumn(ref dgvEmployeeList, "Detail_JobFactorPercent", "%TNN", "C", 70, true, true);
            UISetting.AddColumn(ref dgvEmployeeList, "Detail_JobFactor", UpdateUI.GetString("hesothamniennghecanhbao"), "C", 130, true, true);
            UISetting.AddColumn(ref dgvEmployeeList, "DLNhapDung", "Nhập đúng TNN", "C", 110, true, true);

            UISetting.AddColumn(ref dgvEmployeeList, "BHXH", "Tỷ lệ thu BHXH", "C", 110, true, true);
            UISetting.AddColumn(ref dgvEmployeeList, "BHYT", "Tỷ lệ thu BHYT", "C", 110, true, true);
            UISetting.AddColumn(ref dgvEmployeeList, "BHTN", "Tỷ lệ thu BHTN", "C", 110, true, true);

            UISetting.AddColumn(ref dgvEmployeeList, "GhiChu", "Ghi chú", "", 200, true, true);
            UISetting.AddColumn(ref dgvEmployeeList, "Detail_PositionName", "Chức danh", "", 200, true, true);
        }


        private void PageIndex()
        {
            totalPage = itemCount % pageSize > 0 ? (itemCount / pageSize) + 1 : (itemCount / pageSize);
            if (totalPage == 0)
                lblPageIndex.Text = "0" + "/" + totalPage.ToString();
            else
                lblPageIndex.Text = (currPage + 1).ToString() + "/" + totalPage.ToString();
        }
        
        private void LoadData()
        {
            bool retval = false;
            dgvEmployeeList.Rows.Clear();

            //Nếu form bên cảnh báo nghề đã mở = > lấy luôn ko phải gọi qua service nữa
            if (dtDanhSachCanhBaoNghe == null)
            {
                for (int i = 0; i < Program.frmMain.MdiChildren.Length; i++)
                {
                    if (Program.frmMain.MdiChildren[i].Name == "frmDanhSachNgheList")
                    {
                        dtDanhSachCanhBaoNghe = ((frmDanhSachNgheList)Program.frmMain.MdiChildren[i]).dtDanhSachCanhBaoNghe;
                        retval = true;
                        break;
                    }
                }

                //
                if (!retval)
                {
                    DataSet _ds = _wcfEmployee.Employee_DanhSachNgheList(out itemCount, 0, 0, branch_ID, department_ID, currPage, pageSize);
                    dtDanhSachCanhBaoNghe = _ds.Tables[0];
                }
            }
            if (!dtDanhSachCanhBaoNghe.Columns.Contains("Detail_PositionName"))
                dtDanhSachCanhBaoNghe.Columns.Add(new DataColumn("Detail_PositionName"));
            for (int j = 0; j < dtDanhSachCanhBaoNghe.Rows.Count; j++)
            {
                string Position_Name = Detail_Controller.Instance().GetPositionName(Convert.ToDateTime(dtDanhSachCanhBaoNghe.Rows[j]["Detail_FromDate"]), Convert.ToDateTime(dtDanhSachCanhBaoNghe.Rows[j]["Detail_ToDate"]), dtDanhSachCanhBaoNghe.Rows[j]["Detail_InsBookNo"].ToString(), Program.strBranchCode.ToString(), Convert.ToInt32(dtDanhSachCanhBaoNghe.Rows[j]["Employee_ID"]));
                dtDanhSachCanhBaoNghe.Rows[j]["Detail_PositionName"] = Position_Name;
            }
            PageIndex();

            bool hideCheck = false;
            DataRow[] drDanhSachCanhBaoNghe = null;
            

            switch (cboSoThang.SelectedIndex)
            {
                case 1:
                    drDanhSachCanhBaoNghe = dtDanhSachCanhBaoNghe.Select("Detail_TotalMonth = 13");
                    break;
                case 2:
                    drDanhSachCanhBaoNghe = dtDanhSachCanhBaoNghe.Select("Detail_TotalMonth > 13");
                    break;
                default:
                    drDanhSachCanhBaoNghe = dtDanhSachCanhBaoNghe.Select();
                    break;
            }
             
            if(drDanhSachCanhBaoNghe!=null)
            for (int i = 0; i < drDanhSachCanhBaoNghe.Length; i++)
            {
                dgvEmployeeList.Rows.Insert(i, 1);
                dgvEmployeeList.Rows[i].Cells["RowNumber"].Value = i + 1;
                dgvEmployeeList.Rows[i].Cells["Employee_ID"].Value = drDanhSachCanhBaoNghe[i]["Employee_ID"].ToString();
                dgvEmployeeList.Rows[i].Cells["Employee_FullName"].Value = drDanhSachCanhBaoNghe[i]["Employee_FullName"].ToString();
                dgvEmployeeList.Rows[i].Cells["Employee_DateOfBirth"].Value = Convert.ToDateTime(drDanhSachCanhBaoNghe[i]["Employee_DateOfBirth"]).ToString("dd/MM/yyyy");
                dgvEmployeeList.Rows[i].Cells["Detail_InsBookNo"].Value = drDanhSachCanhBaoNghe[i]["Detail_InsBookNo"].ToString();
                dgvEmployeeList.Rows[i].Cells["Detail_FromDate"].Value = Convert.ToDateTime(drDanhSachCanhBaoNghe[i]["Detail_FromDate"]).ToString("MM/yyyy");
                dgvEmployeeList.Rows[i].Cells["Detail_ToDate"].Value = Convert.ToDateTime(drDanhSachCanhBaoNghe[i]["Detail_ToDate"]).ToString("MM/yyyy");
                dgvEmployeeList.Rows[i].Cells["Detail_TotalMonth"].Value = drDanhSachCanhBaoNghe[i]["Detail_TotalMonth"].ToString();
                dgvEmployeeList.Rows[i].Cells["Detail_JobFactorPercent"].Value = drDanhSachCanhBaoNghe[i]["Detail_JobFactorPercent"].ToString();
                dgvEmployeeList.Rows[i].Cells["Detail_JobFactor"].Value = Convert.ToDecimal(drDanhSachCanhBaoNghe[i]["Detail_JobFactor"]).ToString(UIParamater.Cham3So);
                dgvEmployeeList.Rows[i].Cells["DLNhapDung"].Value = Convert.ToBoolean(drDanhSachCanhBaoNghe[i]["DLNhapDung"]) ? "Đúng" : "Sai";
                dgvEmployeeList.Rows[i].Cells["GhiChu"].Value = drDanhSachCanhBaoNghe[i]["GhiChu"].ToString();
                dgvEmployeeList.Rows[i].Cells["Detail_PositionName"].Value = drDanhSachCanhBaoNghe[i]["Detail_PositionName"].ToString();
                //
                hideCheck = !Convert.ToBoolean(drDanhSachCanhBaoNghe[i]["DLNhapDung"]) || Convert.ToInt32(drDanhSachCanhBaoNghe[i]["Detail_TotalMonth"]) !=13;
                dgvEmployeeList.Rows[i].Cells[0].ReadOnly = (hideCheck ? true : false);
                if (hideCheck)
                    dgvEmployeeList.Rows[i].DefaultCellStyle.BackColor = Color.Yellow;

                //Tỷ lệ thu
                dgvEmployeeList.Rows[i].Cells["BHXH"].Value = drDanhSachCanhBaoNghe[i]["Premium_Rate"];
                dgvEmployeeList.Rows[i].Cells["BHYT"].Value = drDanhSachCanhBaoNghe[i]["Premium_MedicalRate"];
                dgvEmployeeList.Rows[i].Cells["BHTN"].Value = drDanhSachCanhBaoNghe[i]["Premium_UnemployedRate"];
            }
            //dgvEmployeeList.AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.AllCells;
        }

        private void GetSelectedID()
        {
            dgvEmployeeList.EndEdit();
            foreach (DataGridViewRow row in dgvEmployeeList.Rows)
            {
                if (Convert.ToBoolean(row.Cells["Check"].Value))
                {
                    if (!_selectedID.Contains("," + row.Cells["Employee_ID"].Value.ToString() + ","))
                        _selectedID += row.Cells["Employee_ID"].Value.ToString() + ",";
                }
                else
                {
                    string sOld = "," + row.Cells["Employee_ID"].Value.ToString() + ",";
                    if (_selectedID.Contains(sOld))
                        _selectedID = _selectedID.Replace(sOld, ",");
                }
            }
        }

        private void btnFisrt_Click(object sender, EventArgs e)
        {
            GetSelectedID();
            currPage = 0;
            LoadData();
        }

        private void btnNext_Click(object sender, EventArgs e)
        {
            GetSelectedID();
            if (totalPage > 0 && currPage < totalPage - 1)
                currPage++;
            LoadData();
        }

        private void btnPrevious_Click(object sender, EventArgs e)
        {
            GetSelectedID();
            if (currPage > 0)
                currPage--;
            LoadData();
        }

        private void btnLast_Click(object sender, EventArgs e)
        {
            GetSelectedID();
            if (totalPage != 0)
                currPage = totalPage - 1;
            LoadData();
        }

        private void dgvEmployeeList_CellDoubleClick(object sender, DataGridViewCellEventArgs e)
        {
            dlgResult = DialogResult.OK;
            this.Close();
        }

        private void txtKeyWord_KeyUp(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
                btnFind_Click(null, null);
        }

        private void btnFind_Click(object sender, EventArgs e)
        {
            GetSelectedID();
            currPage = 0;
            LoadData();
        }

        private void btnNew_Click(object sender, EventArgs e)
        {
            GetSelectedID();
            if (dgvEmployeeList.SelectedRows.Count > 0)
                dgvEmployeeList_CellDoubleClick(null, null);

        }

        private void frmChooseEmployeeList_FormClosing(object sender, FormClosingEventArgs e)
        {
            this.DialogResult = dlgResult;
        }

        private void ChkCheckAll_CheckedChanged(object sender, EventArgs e)
        {
            if (chkAll.Checked)
            {
                for (int i = 0; i < dgvEmployeeList.Rows.Count; i++)
                {
                    int SoThang = Convert.ToInt32(dgvEmployeeList.Rows[i].Cells["Detail_TotalMonth"].Value);
                    bool NhapDungDL = Convert.ToString(dgvEmployeeList.Rows[i].Cells["DLNhapDung"].Value) == "Đúng";
                    if( SoThang == 13 && NhapDungDL)
                        dgvEmployeeList.Rows[i].Cells["Check"].Value = true;
                }
            }
            else
            {
                for (int i = 0; i < dgvEmployeeList.Rows.Count; i++)
                {
                    dgvEmployeeList.Rows[i].Cells["Check"].Value = false;
                }
            }
            GetSelectedID();
        }

        private void cboSoThang_SelectedIndexChanged(object sender, EventArgs e)
        {
            LoadData();
        }

        private void btnNew_Click_1(object sender, EventArgs e)
        {
            GetSelectedID();
            if (_selectedID.Split(new string[]{","},StringSplitOptions.RemoveEmptyEntries).Length > 0)
            {
                dlgResult = DialogResult.OK;
                this.Close();
            }
            else
            {
                MessageBox.Show("Chưa có dữ liệu nào được chọn!");
            }
        }
    }
}

